package com.yuanyefu.helloworld;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.util.Date;

/**
 * Created by Administrator on 2017/9/16 0016.
 */
public class TestJNDI {
    private static void bindJndi() throws RemoteException, NamingException {
        LocateRegistry.createRegistry(1099);
        System.setProperty(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.rmi.registry.RegistryContextFactory");
        System.setProperty(Context.PROVIDER_URL,"rmi://localhost:1099");
        class RemoteDate extends Date implements Remote{};
        InitialContext ctx = new InitialContext();
        ctx.bind("java:comp/env/systemStartTime",new RemoteDate());
        ctx.close();
    }
    public static void main(String[] args) throws RemoteException, NamingException {
        bindJndi();
        ApplicationContext context = new ClassPathXmlApplicationContext("/spring/config.xml");
        System.out.println("Get system start time from JNDI: "+context.getBean("systemStartTime"));

    }
}
